let app = getApp();
let API_URL = app.globalData.API_URL;
import dayjs from "../../../common/js/dayjs.js";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        lang: null,
        swiperList: [
            { 
                id:1,
                title_ug: 'بىرىنجى يۈرۈشلۈك',
                title_zh: '套餐一',
                commodity_type: 21,
                cont: [ 
                    {
                        name: 1,
                        biao_ug: 'ئۇچۇر يوللاش',
                        biao_zh: '发布信息',
                    },
                    // {
                    //     name: 1,
                    //     biao_ug: 'ھەقسىز چوققىلاش',
                    //     biao_zh: '置顶',
                    // },
                    { 
                        name: '30',
                        biao_ug: 'ئۇچۇر ئىناۋەتلىك ۋاقىت',
                        biao_zh: '信息有效期',
                    },
                    { 
                        name: 10,
                        biao_ug: 'باھاسى',
                        biao_zh: '价格',
                    }
                ]
            },
            {
                id:2,
                title_ug: 'ئىككىنجى يۈرۈشلۈك',
                title_zh: '套餐二',
                commodity_type: 22,
                cont: [
                    { 
                        name: 5,
                        biao_ug: 'ئۇچۇر يوللاش',
                        biao_zh: '发布信息',
                    },
                    // {
                    //     name: 2,
                    //     biao_ug: 'ھەقسىز چوققىلاش',
                    //     biao_zh: '置顶',
                    // },
                    { 
                        name: '30',
                        biao_ug: 'ئۇچۇر ئىناۋەتلىك ۋاقىت',
                        biao_zh: '信息有效期',
                    },
                    {
                        name: 45,
                        biao_ug: 'باھاسى',
                        biao_zh: '价格',
                    }
                ]
            },
            { 
                id:3,
                title_ug: 'ئىختىيارىي يۈرۈشلۈك',
                title_zh: '自定义套餐',
                commodity_type: 23,
                cont: [
                    {
                        id: 1,
                        name: 0,
                        biao_ug: 'ئۇچۇر يوللاش',
                        biao_zh: '发布信息',
                    },
                    // { 
                    //     id: 2,
                    //     name: 0,
                    //     biao_ug: 'ھەقسىز چوققىلاش',
                    //     biao_zh: '置顶',
                    // },
                    { 
                        id: 3,
                        name: '30',
                        biao_ug: 'ئۇچۇر ئىناۋەتلىك ۋاقىت',
                        biao_zh: '信息有效期',
                    },
                    { 
                        id: 4,
                        name: 0,
                        biao_ug: 'باھاسى',
                        biao_zh: '价格',
                    }
                ]
            }
        ],
        swiperCurrent: 0,
        buyMask: false,
        iphoneMask: false, // iphone mask
        buyOrder: [],
        validDate: '', // 有效日期
        package: '', // 选择套餐
        commodity_type: '', // 类型代码
        price: 0, // 自定义类型23时的价格
        dawen: [
            {
                ask_ug: 'سوئال: يوللىغان ئۇچۇرنىڭ ئىناۋەتلىك ۋاقتى قانچىلىك ؟',
                ask_zh: '问：发布的信息有效期多久？',
                answer_ug: 'جاۋاب: ھەربىر يوللانغان ئۇچۇرنىڭ ئىناۋاتلىك ۋاقتى 30 كۈن كۈچكە ئىگە.',
                answer_zh: '答：每条信息的有效期30天。'
            },
            {
                ask_ug: 'سوئال: سېتىۋېلىنغان ھەققىنى قايتۇرغىنى بولامدۇ ؟',
                ask_zh: '问：已购买后可以退款吗？',
                answer_ug: 'جاۋاب: سېتىۋېلىنغىنى مەۋھۇم مەھسۇلات بولۇپ، قايتۇرغىنى بولمايدۇ.',
                answer_zh: '答：因这产品属于虚拟产品，不可退款。'
            }
        ]
    },

    // swiper 滑动
    swiperChange(e) {
        this.setData({ swiperCurrent: e.detail.current })
    },

    // 购买权限
    buyAccess(e) {
        let isIphone = app.globalData.isIphone;
        if (!isIphone) {
            let date = Date.parse(new Date());
            let validDate = dayjs(date + 2629743000).format("YYYY-MM-DD");
            this.setData({
                buyMask: true,
                buyOrder: e.currentTarget.dataset.item.cont,
                validDate: validDate,
                commodity_type: e.currentTarget.dataset.item.commodity_type,
                price: e.currentTarget.dataset.item.cont[2].name
            })

            wx.request({
                url: `${API_URL}/createUserAuth`,
                header: app.header(),
                data: '',
                method: 'POST',
                success: function (res) { }
            })
        } else {
            this.setData({ iphoneMask: true })
        }
    },

    // 关闭苹果手机提示
    finish() {
        this.setData({ iphoneMask: false })
    },

    // 取消付款
    cancelBilling() {
        this.setData({ buyMask: false })
    },

    // 自定义选项增加
    plusBTN(e) {
        let num = e.currentTarget.dataset.item.name;
        num = ++num;
        let total= 0;
        if (num >= 11) return;
        if (e.currentTarget.dataset.item.id == 1) {
            total = num * 10;
            this.setData({ ['swiperList[2].cont[0].name']: num, ['swiperList[2].cont[2].name']: total });
        } else {
            total = this.data.swiperList[2].cont[0].name * 5 + num * 3;
            this.setData({ ['swiperList[2].cont[1].name']: num, ['swiperList[2].cont[2].name']: total });
        }
    },

    // 自定义选项减少
    minusBTN(e) {
        let num = e.currentTarget.dataset.item.name;
        num = --num;
        if (num <= -1) return;
        if (e.currentTarget.dataset.item.id == 1) {
            let total = (this.data.swiperList[2].cont[0].name * 10) - 10;
            this.setData({ ['swiperList[2].cont[0].name']: num, ['swiperList[2].cont[2].name']: total });
        } else {
            let total = (this.data.swiperList[2].cont[1].name * 3) - 3 + (this.data.swiperList[2].cont[0].name * 5);
            this.setData({ ['swiperList[2].cont[1].name']: num, ['swiperList[2].cont[2].name']: total });
        }
    },

    // 确认支付
    confirmBuy() {
        if (this.data.buyOrder[2].name == 0) return;
        
        // 支付
        let bodyCon = '';
        switch (this.data.commodity_type) {
            case 21:
                bodyCon = '单次会员';
                break;
            case 22:
                bodyCon = '月度会员';
                break;
            case 23:
                bodyCon = '自定义会员';
                break;
            default:
                break;
        }
        let commodity_type = this.data.commodity_type
        let price = this.data.price * 100
        let data = { bodyCon, commodity_type, price }
        let that = this;

        // 没登录的先登录再付款
        let promise = new Promise(function(resolve, rej) {
            app.login(res => {
                resolve(res)
            })
        })

        promise.then(
            resolve => {
                wx.request({
                    url: `${API_URL}/payments`,
                    data: data,
                    header: app.header(),
                    method: 'post',
                    success: res=> {
                        wx.requestPayment({
                            nonceStr: res.data.nonceStr,
                            package: res.data.package,
                            paySign: res.data.paySign,
                            signType: "MD5",
                            timeStamp: res.data.timeStamp,
                            success(res) {
                                let buyCon = { send_info_count: that.data.buyOrder[0].name, price: that.data.buyOrder[2].name, user_id: wx.getStorageSync('uid') };
                                wx.request({
                                    url: `${API_URL}/updateUserAuth`,
                                    header: app.header(),
                                    data: buyCon,
                                    method: 'POST',
                                    success: function (res) {
                                        that.setData({ buyMask: false })
                                        wx.showToast({
                                            title: "支付成功",
                                            icon: 'success',
                                            duration: 2000,
                                            mask: false
                                        })
                                    }
                                })
                            },
                            fail(res) {
                                console.log("用户取消支付: ", res);
                                that.setData({ buyMask: false })
                                wx.showToast({
                                    title: "用户取消支付",
                                    icon: 'fail',
                                    duration: 2000,
                                    mask: false
                                })
                            }
                        })
                    }
                })
            }
        )
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({ lang: app.globalData.lang, navTitle: "userAuth", navigation: true })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})